package com.pub.cloud.system.status.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class LinuxSystemTool2 {

	
	
	public static void main(String[] args) {
		//CPU使用率：

		InputStream is = null;
		InputStreamReader isr = null;
		BufferedReader brStat = null;
		StringTokenizer tokenStat = null; // 用来分隔String的应用类
		try {
		System.out.println("Get usage rate of CUP : ");

		Process process = Runtime.getRuntime().exec("top -b"); // top命令是Linux下常用的性能分析工具，能够实时显示系统中各个进程的资源使用情况。
		is = process.getInputStream(); // 可以读取新开启的程序的 System.out.print 输出的内容
		isr = new InputStreamReader(is); // 将字节流转换为字符流。
		brStat = new BufferedReader(isr); // BufferedReader 流能够读取文本行
		brStat.readLine();
		brStat.readLine();

		tokenStat = new StringTokenizer(brStat.readLine());
		tokenStat.nextToken();
		System.out.println("用户空间占用CPU的百分比 : " + tokenStat.nextToken());
		tokenStat.nextToken();
		System.out.println("内核空间占用CPU的百分比 : " + tokenStat.nextToken());
		tokenStat.nextToken();
		tokenStat.nextToken();
		tokenStat.nextToken();
		String cpuFree = tokenStat.nextToken();

		System.out.println("空闲CPU百分比 : " + cpuFree);
		//Float free = new Float(0);

		//Float usage=1 - free.floatValue() / 100;
		 
		// 磁盘空间：
		Process pro = null;
		Runtime r = Runtime.getRuntime();
		String command = "df -h";
		pro = r.exec(command);
		BufferedReader in = new BufferedReader(new InputStreamReader(
		pro.getInputStream()));
		//String line = null;
		System.out.println("目前所有文件系统的可用空间及使用情形 Filesystem Size Used Avail Use% Mounted on");
		while ((in.readLine()) != null) {
		// String[] Info = line.split("\\s+");
			System.out.println(in.readLine());
		}
		in.close();
		pro.destroy();
	}catch (Exception e) {
		// TODO: handle exception
	}
}
}